package com.yuhanginfo.modules.sys.service;

import java.util.Date;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.service.CrudService;
import com.yuhanginfo.modules.sys.entity.Goods;
import com.yuhanginfo.modules.sys.entity.SysShoppingCart;
import com.yuhanginfo.modules.sys.utils.UserUtils;
import com.yuhanginfo.modules.sys.dao.GoodsDao;
import com.yuhanginfo.modules.sys.dao.SysShoppingCartDao;

/**
 * 平台商品信息管理Service
 * 
 * @author yinhao
 * @version 2017-10-10
 */
@Service
@Transactional(readOnly = true)
public class GoodsStoreUpDownService extends CrudService<GoodsDao, Goods> {

	@Autowired
	GoodsDao goodsDao;

	@Autowired
	private SysShoppingCartDao shoppingCartDao;
	


	public Page<Goods> findPage(Page<Goods> page, Goods goods) {
		goods.setPage(page);
		return page.setList(goodsDao.findStoreUpDownList(goods));
	}

	@Transactional(readOnly = false)
	public void up(Goods goods){
			if(goods.getStatus() != 1){
				Goods queryGoods = new Goods();
				queryGoods.setId(goods.getId());
				queryGoods.setStatus(1);
				queryGoods.setUpDate(new Date());
				queryGoods.setUpdateDate(new Date());
				queryGoods.setUpdateBy(UserUtils.getUser());
				//对购物车进行操作
				SysShoppingCart shoppingCart = new SysShoppingCart();
				shoppingCart.setGoodsCode(goods.getCode());
				shoppingCart.setStoreCode(goods.getStoreCode());
				shoppingCart.setStatus("1");
				shoppingCartDao.changeStatus(shoppingCart);
				goodsDao.update(queryGoods);
			}
	}
	
	@Transactional(readOnly = false)
	public void down(Goods goods){
		if(goods.getStatus() == 1){
			Goods queryGoods = new Goods();
			queryGoods.setId(goods.getId());
			queryGoods.setStatus(2);
			queryGoods.setUpdateDate(new Date());
			queryGoods.setUpdateBy(UserUtils.getUser());
			goodsDao.update(queryGoods);
		}
	}
	
	@Transactional(readOnly = false)
	public void batchUp(String[] arrayId){
			for(int i=0;i<arrayId.length;i++){
				Goods queryGoods = new Goods();
				queryGoods.setId(arrayId[i]);
				Goods goods = goodsDao.get(queryGoods);
				if(goods != null&&goods.getStatus() != 1){
						queryGoods.setStatus(1);
						queryGoods.setUpDate(new Date());
						queryGoods.setUpdateDate(new Date());
						queryGoods.setUpdateBy(UserUtils.getUser());
						goodsDao.update(queryGoods);
					
				}
			}
	}
	
	@Transactional(readOnly = false)
	public void batchDown(String[] arrayId){
			for(int i=0;i<arrayId.length;i++){
				Goods queryGoods = new Goods();
				queryGoods.setId(arrayId[i]);
				Goods goods = goodsDao.get(queryGoods);
				if(goods != null&&goods.getStatus() == 1){
				
						queryGoods.setStatus(2);
						queryGoods.setUpdateDate(new Date());
						queryGoods.setUpdateBy(UserUtils.getUser());
						goodsDao.update(queryGoods);
					
				}
			}
	}
}